// map.js
import qqmap from '../../../utils/qqmap'

Page({
  data: {
    id: '',
    latitude: 40.060539,
    longitude: 116.343847,
    repairDetail: {},
    markers: [
      {
        id: 1,
        latitude: 40.22077,
        longitude: 116.23128,
        width: 20,
        height: 30
      },
      {
        id: 2,
        latitude: 40.225857999999995,
        longitude: 116.23246699999999,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      }
    ],
    polyline: []
  },
  onLoad ({ id }) {
    // console.log(id)
    this.setData({ id })
    this.getRepairDetail(id)
    this.getPolyline()
  },
  //获取详情
  async getRepairDetail (id) {
    const { data } = await wx.http.get(/repair/ + id)
    // console.log(data)
    this.setData({ repairDetail: data })
  },
  // 调用位置服务（路线规划）
  getPolyline () {
    qqmap.direction({
      mode: 'bicycling',
      from: '40.225857999999995,116.23246699999999',
      to: '40.22077,116.23128',
      success: res => {
        const coors = res.result.routes[0].polyline
        const points = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }
        // 获取经纬度
        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // console.log(points)
        //渲染数据
        this.setData({
          latitude: points[30].latitude,
          longitude: points[30].longitude,
          polyline: [{ points, color: '#087', width: 4 }]
        })
      }
    })
  },
  //取消报修
  async cancelRepair () {
    const { code } = await wx.http.put('/cancel/repaire/' + this.data.id)
    if (code !== 10000) return wx.utils.toast()
    wx.navigateBack({
      delta: 1
    })
  },
  //修改信息
  editRepair () {
    wx.redirectTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.id
    })
  }
})
